<?php

class Game_model extends CI_Model {

	private $SETS_TABLE = 'game_sets';
	private $GAMES_TABLE = 'games';

	/**
	 * Creates a new challenge in the database.
	 * This includes creating the game set to be played based on the given info,
	 * but not the individual games.  @See create_game handles the creation of these individual games.
	 * Data has been already validated by the controller.
	 *
	 * @param $data - Initial Game set information passed by the challenge form
	 * 	Includes:
	 * 		Players playing, number of players (def: 2), game_type (laddder / casual), number of games, etc.
	 *@return ID of the created game set or null in the case of an error
	 *
	 */
	function create_challenge ($data){
		$this->db->insert('game_sets', $data);
		$inserted_id = $this->db->insert_id();

		if (mysql_error() != ''){
			$inserted_id = NULL;
		}

		return $inserted_id;
	}

	/**
	 * Creates a new game with an associated game set.
	 * Automatically fetches the next game number from the game set.
	 *
	 * @param $data Information about the game's fields passed by the form.
	 *
	 * @return Returns the inserted game's id
	 */
	function create_game ($data){
		$this->db->where('game_set_id', $data['game_set_id']);
		$row = $this->db->select_max('set_number');
		$res = $row->row_array();
		if (isset($res['set_number']) && $res['set_number'] != ''){
			$game_number = $res['set_number']+1;
		} else {
			$game_number = 1;
		}

		$data['set_number'] = $game_number;
		$this->db->insert('games', $data);
		$inserted_id = $this->db->insert_id();

		if (mysql_error() != ''){
			$inserted_id = NULL;
		}

		return $inserted_id;
	}
	//checks if replay exists
	function replay_exists($game_id){
		$this->db->select(array(0 => 'replay', 1 => 'name', 2 => 'size', 3 => 'type'));
		$this->db->where('game_id', $game_id);
		$data = $this->db->get($this->GAMES_TABLE);
		if ($data->num_rows == 0) return false;
		$row = $data->row_array();
		if($row['size'] > 0) return true;
		return false;
	}

	//downloads the replay
	function download_replay($game_id){
		$this->db->select(array(0 => 'replay', 1 => 'name', 2 => 'size', 3 => 'type'));
		$this->db->where('game_id', $game_id);
		$data = $this->db->get($this->GAMES_TABLE);
		if ($data->num_rows == 0) return '';
		$row = $data->row_array();
		header('Content-length: '.$row['size']);
		header('Content-type: '.$row['type']);
		header('Content-Disposition: attachement; filename='.$row['name']);
		echo $row['replay'];
		exit;
	}

	private function get_challenge($where_field, $where_condition){
		$where = array(
		$where_field => $where_condition,
			'status' => 'Pending');

		$query_string = 'SELECT * FROM ' . $SETS_TABLE . ' WHERE ';

		$query = $this->db->query($query_string);
	}

	//Gets challenge by account (accessor method)
	function get_challenge_account($account){

	}

	//Gets challenge by id (accessor method)
	function get_challenge_id($id){
		$this->db->where('game_set_id', $id);
		$query = $this->db->get($this->SETS_TABLE);

		if ($query->num_rows == 1)
			return $query->row_array();

		return false;
	}


	//Gets game by set (accessor method)
	function get_game($set_id){
		$this->db->where('game_set_id', $set_id);
		$query = $this->db->get($this->GAMES_TABLE);

		return $query->result_array();
	}





	/**
	 * Updates information about a game.
	 *
	 * @param $id - Game's game id obtained from matching it's set number & game_set id
	 * @param $data - New informations about the game.
	 */
	function update_game($id, $data){
		$this->db->where('game_id', $id);
		$this->db->set('games', $data);

	}



}
